# 定义装饰器 验证登录
import functools
import traceback

from flask import session, g

from models import db
from models.index import User


def verify_login(view_func):
    #  解决 装饰后 改变了函数名 本质是使原函数名指向 原函数
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):

        # 添加登录验证的逻辑
        user_id = session.get('user_id')

        user = None
        if user_id:
            try:
                user = db.session.query(User).filter(User.id == user_id).first()
            except Exception as e:
                print('报错了', e)
                traceback.print_exc()


        g.user = user

        return view_func(*args,**kwargs)

    return  wrapper
